'use strict';

const Service = require('egg').Service;

class StudentService extends Service {
    //查看所有项目
    async getstudent() {
        const student = await this.app.model.Project.findAll({ attributes: ['name'] });
        return {
            code: 20000,
            student
        };
    }
    //查看项目下的班级
    async outclass(project) {
        const student = await this.app.model.Student.findAll({ where: { project_name: project }, attributes: ['class_name'] });
        return {
            code: 20000,
            student
        };
    }
    //查看所属项目班级的详细信息
    async posttudent(student) {
        try {
            const students = await this.app.model.Student.findAll({ where: student, attributes: { exclude: ['projectName', 'className', 'score_grouping', 'work', 'award', 'thousand'] } });
            return {
                code: 20000,
                students
            };
        } catch (error) {
            return await this.app.Lostconnection(error.message);
        }
    }
    //添加学生
    async createstudent(student) {
        try {
            const token = this.ctx.request.header.access_token;
            const user = await this.ctx.app.jwt.verify(token, this.ctx.app.config.jwt.secret);
            const rolename = await this.app.model.Menu.findAll({ where: { rolename_name: user.rolename_name }, attributes: ['roledetails_roled'] });
            console.log(student);
            for (let i = 0; i < rolename.length; i++) {
                if (rolename[i].roledetails_roled == '学生管理') {
                    await this.app.model.Student.create(student);
                    return {
                        code: 20000,
                        msg: '添加成功'
                    }
                }
            }
            return '权限不够'
        } catch (error) {
            return await this.app.Lostconnection(error.message);
        }
    }
}

module.exports = StudentService;
